package com.topisv.tms.web.shipped;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.web.Servlets;

import com.topisv.tms.entity.BackupTask;
import com.topisv.tms.entity.BackupTaskOrdeno;
import com.topisv.tms.entity.BackupTaskSku;
import com.topisv.tms.entity.BatchTask;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ShippedBatch;
import com.topisv.tms.entity.ShippedBox;
import com.topisv.tms.entity.ShippedBoxOrdeno;
import com.topisv.tms.entity.ShippedBoxSkus;
import com.topisv.tms.entity.dto.ShippedBoxDTO;
import com.topisv.tms.rest.entity.Batch;
import com.topisv.tms.rest.entity.forward.ForwardBatch;
import com.topisv.tms.service.BackupTaskOrdenoService;
import com.topisv.tms.service.BackupTaskService;
import com.topisv.tms.service.BackupTaskSkuService;
import com.topisv.tms.service.BatchTaskService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.shipped.ShippedBatchService;
import com.topisv.tms.service.shipped.ShippedBoxOrdenoService;
import com.topisv.tms.service.shipped.ShippedBoxService;
import com.topisv.tms.service.shipped.ShippedBoxSkusService;
import com.topisv.tms.utils.TimeUtil;
import com.topisv.tms.utils.date.TopIsvDateUtils;
import com.topisv.tms.utils.enums.StatusEnums;
import com.topisv.tms.utils.http.HttpPost;
//import com.topisv.tms.utils.http.SynchroShippedBatchQueue;

/**
 * 出库批次管理
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping(value = "/shippedBatch")
public class ShippedBatchController {
	private static int PAGENUMBER = 10;
	@Autowired
	private ShippedBatchService shippedBatchService;
	@Autowired
	private BackupTaskSkuService backupTaskSkuService;
	@Autowired
	private BackupTaskService backupTaskService;
	@Autowired
	private ShippedBoxService shippedBoxService;
	@Autowired
	private ShippedBoxSkusService shippedBoxSkusService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private BatchTaskService batchTaskService;
	@Autowired
	private ShippedBoxOrdenoService shippedBoxOrdenoService;
	@Autowired
	private BackupTaskOrdenoService backupTaskOrdenoService;
	@Autowired
	private EdiOrderService ediOrderService;

	/**
	 * 导出exl表格
	 * 
	 * @param batchcode
	 * @param response
	 */
	@RequestMapping(value = "exportExcel/{backupTaskCode}")
	public void exportExcel(
			@PathVariable("backupTaskCode") String backupTaskCode,
			HttpServletResponse response) {
		List<BackupTaskOrdeno> backupTaskOrdenos = null;
		BackupTask backupTask = null;
		BatchTask batchTask=null;
		try {
			backupTask = backupTaskService.findByBackupTaskCode(backupTaskCode);
			batchTask=batchTaskService.findByBatchTaskCode(backupTask.getBatchTaskCode());
			backupTaskOrdenos = backupTaskOrdenoService
					.findByBackupTaskCode(backupTaskCode);

			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment; filename="
					+ backupTaskCode + ".xls");
			WritableWorkbook book = Workbook.createWorkbook(response
					.getOutputStream());
			WritableSheet sheet = book.createSheet("第一页", 0);

			WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,
					WritableFont.BOLD);
			WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
			wcf_center.setWrap(false); // 文字是否换行
			sheet.mergeCells(0, 0, 30, 0);
			int length = backupTaskOrdenos.size();

			sheet.addCell(new Label(0, 0, batchTask.getAreaGroupName()+"  共计：" + Integer.toString(length)
					+ " 单", wcf_center));

			sheet.setColumnView(0, 22);
			sheet.setColumnView(1, 15);
			sheet.setColumnView(2, 12);
			sheet.setColumnView(3, 18);
			sheet.setColumnView(4, 12);
			sheet.addCell(new Label(0, 1, "出库批次编码"));
			sheet.addCell(new Label(1, 1, "箱子编码"));
			sheet.addCell(new Label(2, 1, "运单号"));
			sheet.addCell(new Label(3, 1, "省份"));
			sheet.addCell(new Label(4, 1, "时间"));

			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			for (int i = 0; i < length; i++) {
				ShippedBoxOrdeno shippedBoxOrdeno = shippedBoxOrdenoService.findByOrdeno(backupTaskOrdenos.get(i).getOrdeno());
				EdiOrder ediOrder = ediOrderService.findByOrdeno(backupTaskOrdenos.get(i).getOrdeno());
				sheet.addCell(new Label(0, i + 2, backupTaskOrdenos.get(i).getBatchCode()));//出库批次编号

				sheet.addCell(new Label(1, i + 2, shippedBoxOrdeno.getBoxCode()));//箱子编码
				sheet.addCell(new Label(2, i + 2, backupTaskOrdenos.get(i).getWaybillNo()));//运单号

				sheet.addCell(new Label(3, i + 2, ediOrder.getProvna()));//省份信息

				String creatTime = format.format(backupTask.getCreateDate());//时间
				sheet.addCell(new Label(4, i + 2, creatTime));
			}
			book.write();
			book.close();
		} catch (Exception e) {
			
		}
	}

	@RequestMapping(value = "ajaxChangeWaybillNo")
	public void ajaxChangeWaybillNo(
			@RequestParam(value = "boxCode") String boxCode,
			@RequestParam(value = "waybillNo") String waybillNo,
			@RequestParam(value = "index") String index,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		ShippedBoxDTO shippedBoxDTO = null;
		ShiroUser user = getShiroUser();
		try {
			json = new JSONObject();
			out = response.getWriter();
			ShippedBox shippedBox = shippedBoxService.findByBoxCode(boxCode);
			if (shippedBox == null) {
				json.put("status", "2");
				json.put("content", "该箱子不存在");
				out.print(json.toString());
				return;
			}
			if ("0".equals(index)) {
				shippedBoxDTO = shippedBoxService.reduceWaybillNo(boxCode,
						waybillNo);
			} else if ("1".equals(index)) {
				shippedBoxDTO = shippedBoxService.addShippedBoxOrdeno(
						shippedBox.getBatchCode(), boxCode, waybillNo,
						user.jobuuid);
			}

			if (shippedBoxDTO == null) {
				json.put("status", "2");
				json.put("content", "该箱子已经删除");
				out.print(json.toString());
				return;
			} else if ("-1".equals(shippedBoxDTO.getStatus())) {
				json.put("status", shippedBoxDTO.getStatus());
				json.put("content", shippedBoxDTO.getReserve());
				out.print(json.toString());
				return;

			}
			json.put("status", "1");
			json.put("content", shippedBoxDTO);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 订单入箱
	 * 
	 * @param batchCode
	 * @param boxCode
	 * @param waybillNo
	 * @param response
	 */
	@RequestMapping(value = "ajaxGetBoxOrdenos")
	public void ajaxGetBoxOrdenos(
			@RequestParam(value = "boxCode") String boxCode,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		ShippedBox shippedBox = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			shippedBox = shippedBoxService.findByBoxCode(boxCode);
			if (shippedBox == null) {
				json.put("status", "-1");
				json.put("content", "该箱子编号不存在");
				out.print(json.toString());
				return;
			}
			json.put("status", "1");
			json.put("content", shippedBox);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 订单入箱
	 * 
	 * @param batchCode
	 * @param boxCode
	 * @param waybillNo
	 * @param response
	 */
	@RequestMapping(value = "ajaxBoxOrdenos")
	public void ajaxBoxOrdenos(
			@RequestParam(value = "batchCode") String batchCode,
			@RequestParam(value = "boxCode") String boxCode,
			@RequestParam(value = "waybillNo") String waybillNo,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		ShiroUser user = getShiroUser();
		try {
			json = new JSONObject();
			out = response.getWriter();
			if ("".equals(boxCode.trim())) {
				json.put("status", "-1");
				json.put("content", "箱子编码不能为空");
				out.print(json.toString());
				return;
			}
			ShippedBoxDTO shippedBoxDTO = shippedBoxService
					.addShippedBoxOrdeno(batchCode, boxCode, waybillNo,
							user.jobuuid);
			if (shippedBoxDTO == null) {
				json.put("status", "-1");
				json.put("content", "该出库批次不存在");
				out.print(json.toString());
				return;
			}
			json.put("status", shippedBoxDTO.getStatus());
			json.put("content", shippedBoxDTO.getReserve());
			json.put("waybillNo", shippedBoxDTO.getIndex());
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 出库批次推送
	 * 
	 * @param backupTaskCode
	 * @param response
	 */
	@RequestMapping(value = "ajaxPutShippedBatch")
	public void ajaxPutShippedBatch(
			@RequestParam(value = "backupTaskCode") String backupTaskCode,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			BackupTask backupTask = backupTaskService.findByBackupTaskCode(backupTaskCode);
			if (backupTask == null) {
				json.put("status", "-1");
				json.put("content", "该拣货任务不存在");
				out.print(json.toString());
				return;
			}
			ShippedBatch shippedBatch = shippedBatchService.findByBatchCode(backupTask.getBatchCode());
			if (shippedBatch == null) {
				json.put("status", "-1");
				json.put("content", "该出库批次不存在");
				out.print(json.toString());
				return;
			}
			if (!"finish".equals(shippedBatch.getBoxStatus())) {
				json.put("status", "-1");
				json.put("content", "该拣货任务还没有完成货品扫描");
				out.print(json.toString());
				return;
			}
			BatchTask batchTask = batchTaskService.findByBatchTaskCode(backupTask.getBatchTaskCode());
			Customers customers = customersService.findByCustomerCode(batchTask.getCustomerCode());
			// 中转发
			if ("forward".equals(shippedBatch.getType())) {
				ForwardBatch forwardBatch = shippedBatchService.getForwardBatch(shippedBatch.getBatchCode(),customers.getCustomerCode());
				json = JSONObject.fromObject(forwardBatch);
				String result = HttpPost.putForwardBatch(customers,json.toString());
				json = JSONObject.fromObject(result);
				if ("1".equals(json.get("status"))) {
					json.put("content", "推送成功");
					shippedBatch.setStatus("finish");
					shippedBatchService.save(shippedBatch);
				} else {
					json.put("content", json.get("message"));
				}
				out.print(json.toString());

			} else {
				Batch batch = shippedBatchService.getBatch(shippedBatch.getBatchCode(), customers.getCustomerCode());
				if (batch == null) {
					json.put("status", "-1");
					json.put("content", "该出库批次还未封箱");
					out.print(json.toString());
					return;
				}
				json = JSONObject.fromObject(batch);
				String result = HttpPost.putShippedBatch(customers,json.toString());
				json = JSONObject.fromObject(result);
				if ("1".equals(json.get("status"))) {
					json.put("content", "推送成功");
					if(!"finish".equals(shippedBatch.getStatus())){
						shippedBatch.setStatus("finish");
						shippedBatchService.save(shippedBatch);
						if("1".equals(customers.getPassBack())){
//							SynchroShippedBatchQueue.putSynchroQueue(shippedBatch.getBackupTaskCode());
						}
					}
				} else {
					json.put("content", json.get("message"));
				}
				out.print(json.toString());
			}

		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 扫描商品
	 * 
	 * @param backupTaskCode
	 * @param skuId
	 * @param response
	 */
	@RequestMapping(value = "ajaxCheckBackupTaskSkus")
	public void ajaxCheckBackupTaskSkus(
			@RequestParam(value = "backupTaskCode") String backupTaskCode,
			@RequestParam(value = "skuId") String skuId,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		String content = "";
		String index = "pending";
		try {
			json = new JSONObject();
			out = response.getWriter();
			BackupTaskSku backupTaskSku = backupTaskSkuService.skuEnchase(backupTaskCode, skuId);
			if (backupTaskSku == null) {
				json.put("status", "-1");
				backupTaskSku = backupTaskSkuService.findByBackupTaskCodeAndSkuId(backupTaskCode, skuId);
				if (backupTaskSku == null) {
					json.put("content", "商品装箱扫描失败:该商品不属于该拣货任务");
				} else {
					json.put("content", "商品装箱扫描失败:该商品已经拣货成功所需数量为:"+ backupTaskSku.getRealQty());
				}
				json.put("index", index);
				out.print(json.toString());
				return;
			}
			BackupTask backupTask = backupTaskService.findByBackupTaskCode(backupTaskCode);
			if ("finish".equals(backupTask.getStatus())) {
				index = "finish";
			}
			if ("finish".equals(backupTaskSku.getStatus())) {
				json.put("status", "1");
				json.put("content",
						"商品装箱扫描成功:该商品已经拣货完成所需数量为:" + backupTaskSku.getRealQty());
				json.put("index", index);
				out.print(json.toString());
				return;
			}
			content = "商品装箱扫描成功:拣货成功还需要该商品"
					+ (backupTaskSku.getRealQty() - backupTaskSku.getCheckQty())
					+ "件";
			json.put("status", "1");
			json.put("content", content);
			json.put("index", index);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 商品封箱
	 * 
	 * @param backupTaskCode
	 * @param response
	 */
	@RequestMapping(value = "ajaxCreateBox")
	public void ajaxCreateBox(
			@RequestParam(value = "backupTaskCode") String backupTaskCode,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			BackupTask backupTask = backupTaskService
					.findByBackupTaskCode(backupTaskCode);
			if (backupTask == null) {
				json.put("status", "-1");
				json.put("content", "该拣货任务不存在");
				out.print(json.toString());
				return;
			}
			ShippedBox shippedBox = shippedBoxService
					.excuteShippedBox(backupTaskCode);
			if (shippedBox == null) {
				json.put("status", "-1");
				json.put("content", "没有指定商品");
				out.print(json.toString());
				return;
			}

			List<BackupTaskSku> backupTaskSkus = backupTaskSkuService
					.findByBackupTaskCodeAndStatus(backupTaskCode, "pending");
			if (backupTaskSkus.isEmpty()) {
				shippedBox.setIndex("1");
			}
			StringBuffer skuLists = new StringBuffer("");
			List<ShippedBoxSkus> shippedBoxSkus = shippedBoxSkusService
					.findByBoxCode(shippedBox.getBoxCode());
			for (ShippedBoxSkus ob : shippedBoxSkus) {
				if ("".equals(skuLists.toString())) {
					skuLists.append(ob.getSkuId());
				} else {
					skuLists.append("," + ob.getSkuId());
				}
			}
			shippedBox.setStatus(skuLists.toString().toUpperCase());
			json.put("status", "1");
			json.put("content", shippedBox);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	/**
	 * 一键装箱
	 * @param backupTaskCode
	 * @param response
	 */
	@RequestMapping(value = "ajaxKeyCreateBox")
	public void ajaxKeyCreateBox(
			@RequestParam(value = "backupTaskCode") String backupTaskCode,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			BackupTask backupTask = backupTaskService.findByBackupTaskCode(backupTaskCode);
			if (backupTask == null) {
				json.put("status", "-1");
				json.put("content", "该拣货任务不存在");
				out.print(json.toString());
				return;
			}
			ShippedBox shippedBox = backupTaskSkuService.skuEnchase(backupTaskCode);
			if (shippedBox == null) {
				json.put("status", "-1");
				json.put("content", "没有指定商品");
				out.print(json.toString());
				return;
			}

			StringBuffer skuLists = new StringBuffer("");
			List<ShippedBoxSkus> shippedBoxSkus = shippedBoxSkusService
					.findByBoxCode(shippedBox.getBoxCode());
			for (ShippedBoxSkus ob : shippedBoxSkus) {
				if ("".equals(skuLists.toString())) {
					skuLists.append(ob.getSkuId());
				} else {
					skuLists.append("," + ob.getSkuId());
				}
			}
			shippedBox.setStatus(skuLists.toString().toUpperCase());
			shippedBox.setIndex("1");
			json.put("status", "1");
			json.put("content", shippedBox);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	

	/**
	 * 待扫描商品列表
	 * 
	 * @param backupTaskCode
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "shippedBoxSkus/{batchCode}")
	public String shippedBatchList(@PathVariable("batchCode") String batchCode,
			Model model, ServletRequest request) {
		List<BackupTaskSku> backupTaskSkus = null;
		List<ShippedBoxOrdeno> shippedBoxOrdenos = null;
		List<ShippedBoxOrdeno> wait = null;
		List<ShippedBox> shippedBoxs = null;
		List<ShippedBoxSkus> shippedBoxSkusLists = null;
		int noBoxNumber = 0;
		int boxNumber = 0;
		try {
			ShippedBatch shippedBatch = shippedBatchService .findByBatchCode(batchCode);
			BackupTask backupTask=backupTaskService.findByBackupTaskCode(shippedBatch.getBackupTaskCode());
			if(!"finish".equals(backupTask.getPickStatus())){
				return "redirect:/shippedBatch/shippedBatchList";
			}
			model.addAttribute("shippedBatch", shippedBatch);//
			if ("forward".equals(shippedBatch.getType())) {// 中转发处理方式
				shippedBoxs = shippedBoxService.findByBatchCode(batchCode);// 得到该出库批次的箱子数
				shippedBoxOrdenos = shippedBoxOrdenoService .findByBatchCode(batchCode);
				wait = shippedBoxOrdenoService.findByBatchCodeAndStatus( batchCode, "pending");
				model.addAttribute("shippedBoxOrdenos", shippedBoxOrdenos);
				model.addAttribute("shippedBoxs", shippedBoxs);
				model.addAttribute("wait", wait);
				return "shippedBatch/shippedBoxOrdenos";
			}
			backupTaskSkus = backupTaskSkuService .findByBackupTaskCode(shippedBatch.getBackupTaskCode());
			shippedBoxs = shippedBoxService.findByBatchCode(batchCode);
			shippedBoxSkusLists = shippedBoxSkusService .findByBoxCodeAndBackupTaskCode(null, shippedBatch.getBackupTaskCode());

			for (ShippedBoxSkus obj : shippedBoxSkusLists) {
				noBoxNumber += obj.getRealQty();
			}
			for (ShippedBox obj : shippedBoxs) {
				boxNumber += obj.getSkuQty();
			}
			//将sku转化为大写
			toUpperCaseBox(shippedBoxSkusLists);
			toUpperCaseTask(backupTaskSkus);
			model.addAttribute("backupTaskSkus", backupTaskSkus);
			model.addAttribute("shippedBoxs", shippedBoxs);
			model.addAttribute("noBoxNumber", noBoxNumber);//
			model.addAttribute("boxNumber", boxNumber);//
			model.addAttribute("shippedBoxSkusLists", shippedBoxSkusLists);// 已经扫描但是没有装箱的商品
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}
		return "shippedBatch/shippedBoxSkus";
	}
	
	private void toUpperCaseBox(List<ShippedBoxSkus> shippedBoxSkusLists){
		for(ShippedBoxSkus b:shippedBoxSkusLists){
			if(b.getSkuId()!=null){
				b.setSkuId(b.getSkuId().toUpperCase());
			}
		}
	}
	
	private void toUpperCaseTask(List<BackupTaskSku> backupTaskSkus){
		for(BackupTaskSku b:backupTaskSkus){
			if(b.getSkuId()!=null){
				b.setSkuId(b.getSkuId().toUpperCase());
			}
		}
	}

	/**
	 * 显示未处理的出库批次
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "shippedBatchList")
	public String batchLists(Model model) {
		String status = "pending";// 未处理的拣货任务
		ShiroUser user = getShiroUser();
		List<ShippedBatch> shippedBatchs = null;
		try {
			//派友用户
			if(StatusEnums.user_type_paiu.getStatus().equals(user.userType)){
				shippedBatchs=shippedBatchService.findShippedBatchStatus(user.customers, user.areagroups, status);
			}else if(StatusEnums.user_type_customer.getStatus().equals(user.userType)){//商家用户
				shippedBatchs=shippedBatchService.findByStatusAndCustomerCode(status, user.customerCode);
			}
			
			setAreaGroupName(shippedBatchs);
			model.addAttribute("object", shippedBatchs);
			model.addAttribute("status", 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}
		return "shippedBatch/shippedBatchList";
	}

	private void setAreaGroupName(List<ShippedBatch> shippedBatchs ) throws Exception{
		try {
			if(shippedBatchs==null){
				return;
			}
			for(ShippedBatch b:shippedBatchs){
				BackupTask backupTask=backupTaskService.findByBackupTaskCode(b.getBackupTaskCode());
				if(backupTask!=null){
					BatchTask batchTask=batchTaskService.findByBatchTaskCode(backupTask.getBatchTaskCode());
					if(batchTask!=null){
						b.setAreaGroupName(batchTask.getAreaGroupName());
					}
					
				}
				
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	@RequestMapping(value = "batchLists/{from}")
	public String list(
			@PathVariable("from") String from,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			Model model, ServletRequest request) {
		Date startDate = null;
		Date endDate = null;
		List<ShippedBatch> shippedBatchLists = null;
		ShiroUser user = getShiroUser();
		try {
			Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
			if (StatusEnums.user_type_customer.getStatus().equals(user.userType)) {//商家用户
				searchParams.put("EQ_customerCode", user.customerCode);
				if ("pending".equals(from)) {
					searchParams.put("EQ_status", "pending");
					shippedBatchLists = shippedBatchService.getListShippedBatchs(searchParams);
					setAreaGroupName(shippedBatchLists);
					model.addAttribute("status", 1);
					model.addAttribute("object", shippedBatchLists);
					
				} else {
					startDate = TimeUtil.getDate(from);
					endDate = TopIsvDateUtils.addDays(startDate, 1);
					Page<ShippedBatch> pageShippedBatchs = shippedBatchService.getPageShippedBatchs(searchParams, pageNumber,PAGENUMBER, sortType, startDate, endDate);
					setAreaGroupName(pageShippedBatchs.getContent());
					model.addAttribute("from", from);
					model.addAttribute("status", 0);
					model.addAttribute("object", pageShippedBatchs);
				}
			}else if(StatusEnums.user_type_paiu.getStatus().equals(user.userType)){//派友用户
				if ("pending".equals(from)) {
					String status= "pending";
					shippedBatchLists = shippedBatchService.findShippedBatchStatus(user.customers, user.areagroups, status);
					setAreaGroupName(shippedBatchLists);
					model.addAttribute("status", 1);
					model.addAttribute("object", shippedBatchLists);
					
				} else {
					startDate = TimeUtil.getDate(from);
					endDate = TopIsvDateUtils.addDays(startDate, 1);
					String type=(String)searchParams.get("EQ_type");//类型
					String userName=(String)searchParams.get("LIKE_username");//用户名
					Page<ShippedBatch> pageShippedBatchs = shippedBatchService.getPageShippedBatchs( pageNumber,PAGENUMBER, sortType, startDate, endDate,type,userName,user.customers,user.areagroups);
					setAreaGroupName(pageShippedBatchs.getContent());
					model.addAttribute("from", from);
					model.addAttribute("status", 0);
					model.addAttribute("object", pageShippedBatchs);
				}
			}
			
			
			model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}
		return "shippedBatch/shippedBatchList";

	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private ShiroUser getShiroUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}

}
